package com.hefei.garden.other.io;

import lombok.SneakyThrows;
import org.springframework.util.StopWatch;

import java.io.*;

/**
 * io 流测试使用
 */
public class StreamMain {

    private static final int BYTES = 8 * 1024;
    private static final int CHARS = 8 * 1024;
    private static final int IO_STOP_FLAG = -1;
    private static final String BASE_PATH = "C:/Users/yuxiuchen/Desktop/test/";

    private static String getAbsoluteFilePath(String filePath) {
        return BASE_PATH.concat(filePath);
    }

    public static void main(String[] args) {
//        inputStreamTest();
//        outputStreamTest();
//        inAndOutputStreamTest();
//        inputReaderTest();
//        outputWriteTest();
//        inputStreamBufferTest();
//        outputStreamBufferTest();
//        inputReaderBufferTest();
//        outputWriterBufferTest();
//        randomStreamTest();
    }

    /**
     * 一些工具类的使用
     */
    @SneakyThrows
    public static void ioToolsTest() {

    }

    /**
     * 随机流
     */
    @SneakyThrows
    public static void randomStreamTest() {
        RandomAccessFile randomAccessFile = new RandomAccessFile(getAbsoluteFilePath("InputStream.txt"), "rw");
        // 绝对定位，相对于0位置跳过三个字节
        randomAccessFile.seek(3);
        randomAccessFile.seek(6);
        // 在seek的基础上在跳过三个字节
        randomAccessFile.skipBytes(3);
        long filePointer = randomAccessFile.getFilePointer();
        // 当前随机流指针位置
        System.out.println(filePointer);
        byte[] bytes = new byte[BYTES];
        int len = randomAccessFile.read(bytes);
        // 读取的时候在跳过三个字节
        System.out.println(new String(bytes, 3, len));
        randomAccessFile.close();

    }

    /**
     * 缓冲字符输出流
     */
    @SneakyThrows
    public static void outputWriterBufferTest() {
        FileWriter fileWriter = new FileWriter(getAbsoluteFilePath("stream/InputStream_copy5.txt"), true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write("测试字符输出流");
        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();
    }

    /**
     * 缓冲字符输入流
     */
    @SneakyThrows
    public static void inputReaderBufferTest() {
        FileReader fileReader = new FileReader(getAbsoluteFilePath("stream/InputStream.txt"));
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        char[] chars = new char[CHARS];
        while (bufferedReader.read(chars, 0, chars.length) != IO_STOP_FLAG) {
            System.out.print(new String(chars));
        }
        bufferedReader.close();
        fileReader.close();
    }

    /**
     * 缓冲字节输入输出流复制
     */
    @SneakyThrows
    public static void outputStreamBufferTest() {
        // 初始化一个流
        InputStream inputStream = new FileInputStream(getAbsoluteFilePath("stream/InputStream.txt"));
        // 使用缓冲封装
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        OutputStream outputStream = new FileOutputStream(getAbsoluteFilePath("stream/InputStream_copy4.txt"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        byte[] bytes = new byte[BYTES];
        int len;
        while ((len = bufferedInputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, len);
        }
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        outputStream.close();
    }

    /**
     * 缓冲字节输入流，使用装饰器模式
     */
    @SneakyThrows
    public static void inputStreamBufferTest() {
        // 初始化一个流
        InputStream inputStream = new FileInputStream(getAbsoluteFilePath("stream/InputStream.txt"));
        // 使用缓冲封装
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] bytes = new byte[BYTES];
        while (bufferedInputStream.read(bytes) != IO_STOP_FLAG) {
            System.out.println(new String(bytes));
        }
        bufferedInputStream.close();
        inputStream.close();
    }

    /**
     * 字符输出流并续写
     */
    @SneakyThrows
    public static void outputWriteTest() {
        FileWriter fileWriter = new FileWriter(getAbsoluteFilePath("stream/InputStream_copy3.txt"), true);
        fileWriter.write("测试字符输出流");
        fileWriter.close();
    }

    /**
     * 字符输入流
     */
    @SneakyThrows
    public static void inputReaderTest() {
        FileReader fileReader = new FileReader(getAbsoluteFilePath("stream/InputStream.txt"));
        char[] chars = new char[CHARS];
        while (fileReader.read(chars, 0, chars.length) != IO_STOP_FLAG) {
            System.out.print(new String(chars));
        }
        fileReader.close();
    }

    /**
     * 测试使用字节流复制
     */
    @SneakyThrows
    public static void inAndOutputStreamTest() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("文件复制");
        InputStream inputStream = new FileInputStream(getAbsoluteFilePath("stream/elasticsearch-7.4.0.zip"));
        OutputStream outputStream = new FileOutputStream(getAbsoluteFilePath("stream/elasticsearch-7.4.0_copy1.zip"));
        byte[] bytes = new byte[BYTES];
        int limit;
        while ((limit = inputStream.read(bytes)) != IO_STOP_FLAG) {
            outputStream.write(bytes, 0, limit);
        }
        inputStream.close();
        outputStream.close();
        stopWatch.stop();
        // 打印执行的时间
        System.out.println(stopWatch.prettyPrint());
    }

    /**
     * 字节输出流
     */
    @SneakyThrows
    public static void outputStreamTest() {
        OutputStream outputStream = new FileOutputStream(getAbsoluteFilePath("stream/InputStream_copy1.txt"));
        outputStream.write("你好啊".getBytes());
        outputStream.close();
    }

    /**
     * 字节输入流
     */
    @SneakyThrows
    public static void inputStreamTest() {
        InputStream inputStream = new FileInputStream(getAbsoluteFilePath("stream/InputStream.txt"));
        byte[] bytes = new byte[BYTES];
        while (inputStream.read(bytes) != IO_STOP_FLAG) {
            System.out.println(new String(bytes));
        }
        inputStream.close();
    }

}
